﻿using AntdUI;
using Flurl;
using Flurl.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Xunit;

namespace TCOChart
{
    public partial class FormFlurl : Form
    {
        public FormFlurl()
        {
            InitializeComponent();
            InitFlurl();
        }
        public void InitFlurl()
        {

            var url = "https://some-api.com"
                .AppendPathSegment("endpoint")
                .SetQueryParams(new
                {
                    //api_key = _config.GetValue<string>("MyApiKey"),
                    max_results = 20,
                    //q = "I'll get encoded!",
                    
                })
                .SetFragment("after-hash");
            url.SetQueryParam("name", "val!");
            url.AppendQueryParam("name", "val2");  // 相同key不会被覆盖
            url.SetQueryParam("x", new[] { 1, 2, 3, 4 });

            var url2 = url.Clone().AppendPathSegment("next");

            label1.Text = url.ToString();
            //label2.Text = url2.ToString();
            Assert.Equal("https", url.Scheme);

            _ = Test();


        }

        public async Task Test()
        {
            var result = await "http://172.16.25.132:8002/api/code/getId"
            //.AppendPathSegment("GetA")
            .GetAsync();

            //.WithTimeout(TimeSpan.FromSeconds(1))
            //.GetJsonAsync<Post>();
            if (result.StatusCode == 200)
            {
                // 将响应内容解析为 JSON 对象
                var data = await result.GetJsonAsync<Post>();
                label2.Text = data.Title;

                //label2.Text = result.Title;
            }
            else
            {
                label2.Text = ($"请求失败，状态码: {result.StatusCode}");
            }
        }

        public class Post
        {
            public int UserId { get; set; }
            public int Id { get; set; }
            public string Title { get; set; }
            public string Body { get; set; }
        }
    }
}
